package cn.tianyigps.tianyigpsynclean.hanlder;

import cn.tianyigps.tianyigpskafka.task.manger.NewTaskManager;
import cn.tianyigps.tianyigpspublic.bean.EquipmentLastData;
import cn.tianyigps.tianyigpspublic.param.bo.Task;
import cn.tianyigps.tianyigpspublic.param.constant.EquipmentReceiveConstant;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: WEI.YUAN
 * @Descirption:任务下发
 * @Date: 2019/4/28_10:56
 */
@Component
@Slf4j
public class HandOutTaskHandler {
    @Autowired
    NewTaskManager newTaskManager;
    private static ObjectMapper OM = new ObjectMapper();
    /**
     * 任务分发
     *
     * @param handLastData
     */
    public void handOutTask(EquipmentLastData handLastData) {
        Task task = new Task();
        Map<String, String> params = new HashMap<>();
        handLastData.getParams().forEach((key, value) -> {
            params.put(key, value.getValue());
        });
        handLastData.getMaps().forEach((key, value) -> {
            params.put(key, String.valueOf(value));
        });
        params.put(EquipmentReceiveConstant.DeviceCode,handLastData.getEquipmentId());
        task.setParams(params);
        try {
            newTaskManager.distributeTask("yndistributetask", OM.writeValueAsString(task));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("任务发送失败:{}", e);
        }
    }
}
